/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package za.co.pas.business.view.model.combo;

import java.util.LinkedList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;
import za.co.pas.business.constant.Constant;
import za.co.pas.business.data.Country;
import za.co.pas.business.data.Province;
import za.co.pas.business.model.CountryDataModel;
import za.co.pas.business.mvc.BusinessMVC;

/**
 *
 * @author alabuschagne
 */
public class ProvinceComboModel implements ComboBoxModel
{
    private CountryDataModel countryDataModel = (CountryDataModel) Constant.mvc.getModel(BusinessMVC.COUNTRY_DATA_MODEL);
//    private List<Province> provinces = new LinkedList<Province>();
    private Province selectedProvince = null;
    private Country country;
    @Override
    public void setSelectedItem(Object anItem)
    {
        selectedProvince = (Province)anItem;
    }

    @Override
    public Object getSelectedItem()
    {
        return selectedProvince;
    }

    @Override
    public int getSize()
    {
        if((countryDataModel != null) && (country != null))
        {
            List<Province> bt = countryDataModel.getProvincesUsingCountry(country);
            if(bt != null)
            {
                return bt.size();
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }

    @Override
    public Object getElementAt(int index)
    {
        if((countryDataModel != null) && (country != null))
        {
            List<Province> bt = countryDataModel.getProvincesUsingCountry(country);
            if((bt != null) && (!bt.isEmpty()))
            {
                return bt.get(index);
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

    @Override
    public void addListDataListener(ListDataListener l)
    {//Do nothing?
    }

    @Override
    public void removeListDataListener(ListDataListener l)
    {//Do nothing?
    }

    /**
     * @return the country
     */
    public Country getCountry()
    {
        return country;
    }

    /**
     * @param country the country to set
     */
    public void setCountry(Country country)
    {
        this.country = country;
    }
}
